package com.wskj.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.EncryptUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.system.bo.SysAboutUsAddBo;
import com.wskj.system.bo.SysAboutUsEditBo;
import com.wskj.system.bo.SysAboutUsQueryBo;
import com.wskj.system.domain.SysAboutUs;
import com.wskj.system.mapper.SysAboutUsMapper;
import com.wskj.system.service.ISysAboutUsService;
import com.wskj.system.vo.web.SysAboutUsVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 关于我们Service业务层处理
 *
 * @author wsdz
 * @date 2022-12-13
 */
@Service
public class SysAboutUsServiceImpl extends ServicePlusImpl<SysAboutUsMapper, SysAboutUs> implements ISysAboutUsService {

    @Override
    public SysAboutUsVo queryById(Long usId){
        return getVoById(usId, SysAboutUsVo.class);
    }

    @Override
    public TableDataInfo<SysAboutUsVo> queryPageList(SysAboutUsQueryBo bo) {
        PagePlus<SysAboutUs, SysAboutUsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysAboutUsVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysAboutUsVo> queryList(SysAboutUsQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysAboutUsVo.class);
    }

    private LambdaQueryWrapper<SysAboutUs> buildQueryWrapper(SysAboutUsQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysAboutUs> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), SysAboutUs::getTitle, bo.getTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), SysAboutUs::getContent, bo.getContent());
        lqw.eq(bo.getStatus() != null, SysAboutUs::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysAboutUsAddBo bo) {
        SysAboutUs add = BeanUtil.toBean(bo, SysAboutUs.class);
        validEntityBeforeSave(add);
        if(Objects.nonNull(bo) && StrUtil.isNotBlank(bo.getReplaceContent())){
            String content = EncryptUtils.aesDecryptForFront(bo.getReplaceContent(),EncryptUtils.KEY_DES);
            add.setContent(content);
        }
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysAboutUsEditBo bo) {
        SysAboutUs update = BeanUtil.toBean(bo, SysAboutUs.class);
        validEntityBeforeSave(update);
        if(Objects.nonNull(bo) && StrUtil.isNotBlank(bo.getReplaceContent())){
            String content = EncryptUtils.aesDecryptForFront(bo.getReplaceContent(),EncryptUtils.KEY_DES);
            update.setContent(content);
        }
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysAboutUs entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importSysAboutUs(List<SysAboutUsVo> sysAboutUsList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(sysAboutUsList), "导入数据不能为空");
        int successNum = 0;
        for (SysAboutUsVo sysAboutUs : sysAboutUsList) {
            SysAboutUs add = BeanUtil.toBean(sysAboutUs, SysAboutUs.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", sysAboutUsList.size(), successNum);
        if (successNum != sysAboutUsList.size()) {
            msg = msg + String.format("失败 %d 条！", sysAboutUsList.size() - successNum);
        }
        return msg;
    }
}
